window.rootPath = (function(src) {
	src = document.scripts[document.scripts.length - 1].src;
	return src.substring(0, src.lastIndexOf("/") + 1);
})();
//config的设置是全局的
layui.config({
	base: rootPath + "modules/" //拓展模块的根目录
	, version: false
	, debug: true
});
layui.use(['jquery'], function() {
	var $ = layui.jquery;
	$.ajaxSetup({
		beforeSend: function(xhr, settings) {
			// header里加请求头参数Authorization="Bearer " + access_token，用于鉴权
			var access_token = layui.data('platform').access_token;
			if (access_token) {
				xhr.setRequestHeader('Authorization', "Bearer " + access_token)
			}
			// post或put使用application/json类型
			if (settings.type.toLowerCase() == 'post'
				|| settings.type.toLowerCase() == 'put') {
				xhr.setRequestHeader("Content-Type",
					"application/json; charset=utf-8");
			}
		},
		complete: function(xhr, data) {
			// header里获取响应头access_token，并缓存
			if (xhr.status === 200) {
				var access_token = xhr.getResponseHeader("access_token");
				if (access_token) {
					layui.data('platform', {
						key: "access_token",
						value: access_token
					})
				}
			}
		}
	});
});
